package si.ibloc.cms.logic.impl;

import javax.persistence.EntityManager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.RaiseEvent;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.bpm.BeginTask;
import org.jboss.seam.annotations.bpm.EndTask;
import org.jboss.seam.bpm.PooledTask;
import org.jboss.seam.log.Log;
import si.ibloc.cms.model.Content;

/**
 * User: maticm<br/>
 * Date: 4.9.2007
 * <p/>
 * <p>(C) Genera Lynx, d.o.o.</p>
 */
@Name("contentPublishing")
@Scope(ScopeType.STATELESS)
public class ContentPublishing {
   @Logger private transient Log log;

   @In private EntityManager entityManager;
   @In(scope = ScopeType.BUSINESS_PROCESS) private Content pubContent;
   @In private PooledTask pooledTask;

   @BeginTask
   public void startPublish() {
      pooledTask.assignToCurrentActor();
      log.info("Starting publishing");
   }

   @EndTask(transition = "publish")
   @RaiseEvent("newContentSaved")
   public String commitPublish() {
      log.info("Publishing content " + pubContent);
      entityManager.persist(pubContent);
      return "home";
   }

   @EndTask(transition = "cancel")
   public String cancelPublish() {
      pooledTask.assignToCurrentActor();
      log.info("Discarding content: " + pubContent);
      return "home";
   }

   @BeginTask @EndTask(transition = "cancel")
   public String cancelPublishDirectly() {
      return "home";
   }
}
